package logic

import (
	"context"
	"errors"
	"video-guard/models/mysql"

	"video-guard/videorpc/internal/svc"
	"video-guard/videorpc/videorpc"

	"github.com/zeromicro/go-zero/core/logx"
)

type NickNameUpdateLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewNickNameUpdateLogic(ctx context.Context, svcCtx *svc.ServiceContext) *NickNameUpdateLogic {
	return &NickNameUpdateLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *NickNameUpdateLogic) NickNameUpdate(in *videorpc.NickNameUpdateRequest) (*videorpc.NickNameUpdateResponse, error) {
	// todo: add your logic here and delete this line

	var user mysql.UserInfo

	err := user.FirstUserById(mysql.DB, in.UserId)
	if err != nil {
		return nil, errors.New("该用户不存在")
	}

	user.NickName = in.NickName

	err = user.UserUpdate(mysql.DB, in.UserId)
	if err != nil {
		return nil, errors.New("修改失败")
	}

	return &videorpc.NickNameUpdateResponse{
		UserId: int64(user.ID),
	}, nil
}
